/**
 * Created by zhaojing on 2017/10/25.
 */
import tpl from './tpl.html';
import VSelect from '../../../../../../grsc-base/component/Select.vue';
import {REG_ADDR,REG_EN} from '../../../../../../grsc-base/common/constants';
import filter from '../../../../../../grsc-base/util/filter';
import Help from '../../../../../../grsc-base/component/Help.vue';
import commonUtil from '../../../../../../grsc-base/common/commonUtil';
import Validator from '../../../../../../grsc-base/component/Validator/main';
import Loading from '../../../../../../grsc-base/component/Loading.vue';
import fileUpload from '../../../../../../grsc-base/component/Upload/Fileupload.vue';
import service from '../../service'
import unsupportedCountries from './../unsupportedCountries/main'

export default {
    name: 'addressVerification',
    template: tpl,
    components: {
        Validator: Validator,
        Loading: Loading,
        Help,
        fileUpload,
        unsupportedCountries
    },
    data() {
        return {
            isLoading: false,
            showUnsupportedCountries:false,
            residencePhotoTip:false,

            form: {
                countryId: '',
                country: '',
                city: '',
                address: '',
                postcode: '',
                residencePhoto: [],
                residenceTranslate: [],
                agree: false
            },
            rules: {
                city: {
//                        tip: 'Please enter City.',
                    required: true,
                    requiredTip: this.$t('certification.verify.address.cityTip'),
                    pattern: /^\S+.*$/,
                    patternTip: this.$t('certification.verify.address.errCityTip')

                },
                address: {
//                        tip: 'Please enter Address.',
                    required: true,
                    requiredTip: this.$t('certification.verify.address.addressTip'),
                    pattern:  /^\S+.*$/,
                    patternTip: this.$t('certification.verify.address.errAddressTip')
                },
                postcode: {
//                        tip: 'Please enter Postal/Zip Code.',
                    required: true,
                    requiredTip: this.$t('certification.verify.address.postTip'),
                    pattern: REG_EN,
                    patternTip: this.$t('certification.verify.address.errPostTip')
                }
            },
            isRequesting: false
        }
    },

    computed:{

    },
    created() {
        this.fetchCountry();
    },
    watch: {
        'form.agree'(newVal, oldVal) {
//                console.log('form.agree:', newVal);
        }
    },
    methods: {
        changeAgree($event) {
            if ($event.target.checked === true) {
                this.form.agree = true;
            } else {
                this.form.agree =  false;
            }
        },
        fetchCountry(){
            this.loading = true;
            service.getBasicAuthInfo().done((rep) => {
                this.form.countryId = rep.data.userIdentification.countryId;
                this.form.country = rep.data.userIdentification.country;
            }).always(() => {
                this.loading = false;
            })
        },

        checkPhoto(){
            let photoFlag = true;
            if (this.form.residencePhoto.length < 1) {
                this.residencePhotoTip = true;
                photoFlag = false;
            }
            return photoFlag;
        },
        submit(){
            if (this.rules.$checkAll() === false) {
                this.checkPhoto();
                return;
            } else {
                if (!this.checkPhoto()) {
                    return;
                }
            }

            this.isRequesting = true;
            let param = {
                countryId: this.form.countryId,
                country: this.form.country,
                city: this.form.city,
                address: this.form.address,
                postcode: this.form.postcode,
                residencePhoto: this.form.residencePhoto,
                residenceTranslate: this.form.residenceTranslate
            };
            service.level2Auth(param).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    window.success(this.$t('common.success'), () => {
                        this.$emit('goBack');
                    });
                }else if(rep.code === window.CODE.AUTH_RESUBMIT.CODE){
                    window.error(window.CODE.AUTH_RESUBMIT[this.$i18n.locale])
                } else {
                    window.error(rep.msg);
                }
            }).always(() => {
                this.isRequesting = false;
            });

        },

        deleteFile(fileList, listName){
            this.form[listName]=fileList;
        },

        onFileClick(listName){
            this[listName+'Tip'] = false;
        },

        onFileUpload: function (fileList, listName) {
            this.form[listName]=fileList;
        },

        changeUnsupportCountry(){
            this.showUnsupportedCountries = !this.showUnsupportedCountries;
        },
    }
}